package com.wh.usercenter3.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.usercenter3.constant.CodeConstant;
import com.wh.usercenter3.exception.BaseException;
import com.wh.usercenter3.pojo.Team;
import com.wh.usercenter3.pojo.User;
import com.wh.usercenter3.pojo.UserTeam;
import com.wh.usercenter3.pojo.dto.QueryTeamDto;
import com.wh.usercenter3.pojo.dto.TeamDto;
import com.wh.usercenter3.pojo.dto.TeamQueryDto;
import com.wh.usercenter3.pojo.vo.NewTeamVo;
import com.wh.usercenter3.pojo.vo.PageDataVo;
import com.wh.usercenter3.response.Result;
import com.wh.usercenter3.response.ResultUtil;
import com.wh.usercenter3.service.TeamService;
import com.wh.usercenter3.service.UserTeamService;
import com.wh.usercenter3.util.TeamUtil;
import com.wh.usercenter3.util.UserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@Tag(name = "组队模块")
@RequestMapping("/team")
public class TeamController {


    @Resource
    private TeamService teamService;

    @Resource
    private UserTeamService userTeamService;

    /**
     * 队伍创建
     * @param dto
     * @param request
     * @return
     */
    @Operation(summary = "队伍创建")
    @PostMapping("/add")
    public Result<NewTeamVo> teamAdd(@RequestBody(required = false) TeamDto dto, HttpServletRequest request) {
        // 简单判空
        if (dto == null) throw new BaseException(CodeConstant.CLIENT_ERR, "参数为空");
        // TeamDto转Team实体类
        Team team = new Team();
        BeanUtils.copyProperties(dto,team);
        return ResultUtil.ok(teamService.teamAdd(team,request));
    }

    /**
     * 队伍更新
     * @param dto
     * @param request
     * @return
     */
    @Operation(summary = "队伍更新")
    @PutMapping("/update")
    public Result teamUpdate(@RequestBody(required = false) TeamDto dto, HttpServletRequest request) {
        // 简单判空
        if (dto == null) throw new BaseException(CodeConstant.CLIENT_ERR, "参数为空");
        // TeamDto转Team实体类
        Team team = new Team();
        BeanUtils.copyProperties(dto,team);
        // 调用service层
        teamService.teamUpdate(team,request);
        return ResultUtil.ok();
    }

    /**
     * 根据id查询单条数据
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询单条数据")
    @GetMapping("/get")
    public Result<NewTeamVo> getById(long id) {
        if (id!=0) throw new BaseException(CodeConstant.CLIENT_ERR, "id为0");
        Team team = teamService.getById(id);
        if (team!=null) throw new BaseException(CodeConstant.SERVICE_ERR, "查询失败");
        NewTeamVo newTeamVo = new NewTeamVo();
        BeanUtils.copyProperties(team,newTeamVo);
        return ResultUtil.ok(newTeamVo);
    }

    /**
     * 模糊查询
     * @param dto
     * @param request
     * @return
     */
    @Operation(summary = "模糊查询")
    @PostMapping("/list")
    public Result<List<NewTeamVo>> teamList(@RequestBody QueryTeamDto dto, HttpServletRequest request) {
        // 简单判空
        if (dto == null) throw new BaseException(CodeConstant.CLIENT_ERR, "参数为空");
        // 简单判断是否登录
        User loginUser = UserUtil.userByLogin(request);
        if (loginUser==null) throw new BaseException(CodeConstant.CLIENT_ERR, "未登录");
        // 服务层
        List<NewTeamVo> list = teamService.teamList(dto, loginUser);
        if (list==null|| list.isEmpty()) throw new BaseException(CodeConstant.SERVICE_ERR, "查询为空");
        return ResultUtil.ok(list);
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Operation(summary = "分页查询")
    @PostMapping("/list/page")
    public Result<PageDataVo<List<NewTeamVo>>> listPage(@RequestBody QueryTeamDto dto, HttpServletRequest request) {
        log.info("参数为：{}",dto);
        // 简单判空
        if (dto == null) throw new BaseException(CodeConstant.CLIENT_ERR, "参数为空");
        // 简单判断是否登录
        User loginUser = UserUtil.userByLogin(request);
        if (loginUser==null) throw new BaseException(CodeConstant.CLIENT_ERR, "未登录");
        // 服务层
        List<NewTeamVo> list = teamService.teamList(dto, loginUser);
        if (list==null|| list.isEmpty()) throw new BaseException(CodeConstant.SERVICE_ERR, "查询为空");
        // 封装
        PageDataVo<List<NewTeamVo>> pageDataVo = TeamUtil.setPageDataVo(list, dto.getPageNum(), dto.getPageSize());
        return ResultUtil.ok(pageDataVo);
    }

    /**
     *  加入队伍
     * @param userId
     * @param teamId
     * @return
     */
    @Operation(summary = "加入队伍")
    @GetMapping("/join")
    public Result teamJoin(long userId,long teamId,String password) {
        if (userId == 0 || teamId == 0) {
            throw new BaseException(CodeConstant.CLIENT_ERR, "参数id不能为0");
        }
        if (!teamService.teamJoin(userId, teamId,password)) throw new BaseException(CodeConstant.SERVICE_ERR, "加入失败");
        return  ResultUtil.ok();

    }



    /**
     * 退出队伍
     * @param userId
     * @param teamId
     * @return
     */
    @Operation(summary = "退出队伍")
    @GetMapping("/quit")
    public Result teamQuit(long userId,long teamId) {
        if (userId == 0 || teamId == 0) {
            throw new BaseException(CodeConstant.CLIENT_ERR, "参数不能为0");
        }
        if (!teamService.teamQuit(userId, teamId)) throw new BaseException(CodeConstant.SERVICE_ERR, "user未加入队伍");
        return  ResultUtil.ok();
    }

    @Operation(summary = "删除队伍")
    @GetMapping("/delete")
    public Result teamDel(long teamId, long userId, HttpServletRequest request) {
        if (teamId == 0) {
            throw new BaseException(CodeConstant.CLIENT_ERR, "队伍id不能为0");
        }
        User loginUser = UserUtil.userByLogin(request);
        if (userId == 0&&loginUser.getUserRole()!=1)
            throw new BaseException(CodeConstant.CLIENT_ERR, "用户无权限");
        if (!teamService.teamDel(teamId, userId))
            throw new BaseException(CodeConstant.CLIENT_ERR, "删除队伍失败");
        return ResultUtil.ok();
    }

}
